﻿using System;
using System.IO;
using System.Web;
using System.Xml;

namespace GateKeeper
{
    public class NewXmlFile
    {
        /// <summary>
        /// Creates a new xml file
        /// </summary>
        public static void Create(XmlFileType fileType, string path)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            MemoryStream ms = new MemoryStream();

            GateKeeperModule.log.Debug("Adding new xml elements to memory stream");
            using (XmlWriter writer = XmlWriter.Create(ms, settings))
            {
                writer.WriteStartDocument(true);
                writer.WriteStartElement("gatekeeper");

                switch (fileType)
                {
                    case XmlFileType.BlackList:
                        writer.WriteElementString("ipaddress", string.Empty);
                        writer.WriteElementString("useragent", string.Empty);
                        break;
                    case XmlFileType.WhiteList:
                        writer.WriteElementString("ipaddress", string.Empty);
                        writer.WriteElementString("useragent", string.Empty);
                        writer.WriteElementString("url", string.Empty);
                        break;
                    case XmlFileType.HoneyPotList:
                        writer.WriteElementString("honeypot", string.Empty);
                        break;
                    case XmlFileType.HttpBLList:
                        writer.WriteElementString("httpbl", string.Empty);
                        break;
                    case XmlFileType.ProxyBLList:
                        writer.WriteElementString("proxybl", string.Empty);
                        break;
                    case XmlFileType.DroneBLList:
                        writer.WriteElementString("dronebl", string.Empty);
                        break;
                    default:
                        break;
                }

                writer.WriteEndElement();
                writer.WriteEndDocument();
            }

            // Build file name from fileType
            //string fileName = string.Format("~/app_data/gk{0}.xml", fileType.ToString());
            //string path = HttpContext.Current.Server.MapPath(fileName);

            GateKeeperModule.log.DebugFormat("New xmlFile path : {0}", path);
            Utils.SaveStream(ms, path);
        }
    }
}
